package gam.n1.achievement;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public final class AchievementManager implements iAchievementManager{
    private final Map<String, List<iAchievement>> as;
    private List<iAchievement> rule;
    public AchievementManager(){
        this(null);
    }
    public AchievementManager(List<iAchievement> rule) {
        this.as = new ConcurrentHashMap<>();
        this.rule = rule;
    }

    @Override
    public boolean addRule(iAchievement achievement) {
        if (rule == null){
            rule = new ArrayList<>();
        }
        return rule.add(achievement);
    }


    @Override
    public iAchievement getAchievementByUUID(String uuid, String achievementName) {
        if (as.size() > 0){
            List<iAchievement> var1 = as.get(uuid);
            if (var1 != null){
                for (iAchievement var3 : var1) {
                    if (var3.name().equals(achievementName)) return var3;
                }
            }
        }
        return null;
    }

    @Override
    public <T> iAchievement<T> getAchievementByUUID(String uuid, String achievementName, Class<T> type) {
        Objects.requireNonNull(uuid);
        Objects.requireNonNull(achievementName);
        Objects.requireNonNull(type);
        if (as.size() > 0){
            List<iAchievement> var1 = as.get(uuid);
            if (var1 != null){
                for (iAchievement var3 : var1) {
                    if (var3.name().equals(achievementName) && var3.t().equals(type)) return var3;
                }
            }
        }
        return null;
    }

    @Override
    public boolean initUUID(String uuid) {
        if (rule != null && rule.size() > 0){
            addAchievement(uuid,deepCopyOnRule());
        }
        return false;
    }
    private List<iAchievement> deepCopyOnRule(){
        List<iAchievement> t = new ArrayList<>();
        Iterator<iAchievement> var1 = rule.iterator();
        while (var1.hasNext()){
            iAchievement n = var1.next();
            t.add(n.copy());
        }
        return t;
    }

    @Override
    public boolean isCompleteAchievement(iAchievement achievement) {
        return false;
    }

    @Override
    public boolean addAchievement(String uuid, iAchievement achievement) {
        List<iAchievement> _ia = as.computeIfAbsent(uuid, k -> new ArrayList<>());
        return _ia.add(achievement);
    }

    @Override
    public boolean addAchievement(String uuid, List<iAchievement> achievement) {
        as.put(uuid,achievement);
        return true;
    }

    @Override
    public boolean createNewOne(String uuid) {
        as.put(uuid,null);
        return false;
    }

    @Override
    public void deleteOne(String uuid) {
        as.remove(uuid);
    }
}
